package com.hzdesk.media.matvector.koreabatch.client.my_paperless.base;

import android.Manifest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.hzdesk.media.matvector.koreabatch.client.my_paperless.R;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.third.Eyes;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.NetworkUtils;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.VirtualButtonsUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

/**
 * Created by Administrator on 2018/8/8.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public abstract class BaseActivity extends AppCompatActivity {
    private IntentFilter intentFilter;
    ImageView myBaseRlTileIvLeft;
    TextView myBaseRlTileTvTile;
    ImageView myBaseRlTileIvRight;
    TextView myBaseRlTileTvRight;
    RelativeLayout myBaseRlTile;
    FrameLayout baseRlView;  //NetworkBroadcastReceiver

    private View myChildFragmentView;
   /* private BroadcastReceiver networkChangeReceiver = new BroadcastReceiver(){

        @Override
        public void onReceive(Context context, Intent intent) {
            if(NetworkUtils.checkWiFiConnectedState(context) && NetworkUtils.checkMobileConnectedState(context)){
                isWiFiAndMobileConnecte();
            }else if(NetworkUtils.checkWiFiConnectedState(context) && !NetworkUtils.checkMobileConnectedState(context)){
                NoMobileaAndWiFiConnecte();
            }else if(!NetworkUtils.checkWiFiConnectedState(context) && NetworkUtils.checkMobileConnectedState(context)){
                NoWiFiAndMobileConnecte();
            }else {
                isDisConnecte();
            }
        }
    };*/
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Eyes.setStatusBarColor(this, 0xff1D8AFF);
        VirtualButtonsUtils.hideNavKey(this);
        setContentView(R.layout.activity_base);
        findView();

    }

    private void findView() {
        myBaseRlTileIvLeft = findViewById(R.id.activity_base_my_base_rl_tile_iv_left);
        myBaseRlTileTvTile = findViewById(R.id.activity_base_my_base_rl_tile_tv_tile);
        myBaseRlTileIvRight = findViewById(R.id.activity_base_my_base_rl_tile_iv_right);
        myBaseRlTileTvRight =findViewById(R.id.activity_base_my_base_rl_tile_tv_right);
        myBaseRlTile = findViewById(R.id.activity_base_my_base_rl_tile);
        baseRlView = findViewById(R.id.activity_base_rl_view);

        setOnclikedLinstener(myBaseRlTileIvLeft,0);
        setOnclikedLinstener(myBaseRlTileIvRight,1);
        setOnclikedLinstener(myBaseRlTileTvRight,2);


    }
    public void setOnclikedLinstener(View view, final int type){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type){
                    case 0:
                        onClickedIvLeft();
                        break;
                    case 1:
                        onClickedIvRight();
                        break;
                    case 2:
                        onClickedTvRight();

                        break;
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
      /*  intentFilter = new IntentFilter();
        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        registerReceiver(networkChangeReceiver, intentFilter);*/
        myChildFragmentView =  AddLayoutView();
        baseRlView.addView(myChildFragmentView);
        initChildViewDate();
    }

    @Override
    protected void onStop() {
        super.onStop();
      //  unregisterReceiver(networkChangeReceiver);
        if(baseRlView != null){
            baseRlView.removeView(myChildFragmentView);
            myChildFragmentView = null;
        }
    }

    public void isDisConnecte() {
        Toast.makeText(this, "WIFI已断开,移动数据已断开", Toast.LENGTH_SHORT).show();
    }

    public void NoWiFiAndMobileConnecte() {
        Toast.makeText(this, "WIFI已断开,移动数据已连接", Toast.LENGTH_SHORT).show();
    }

    public void NoMobileaAndWiFiConnecte() {
        Toast.makeText(this, "WIFI已连接,移动数据已断开", Toast.LENGTH_SHORT).show();
    }

    public void isWiFiAndMobileConnecte() {
        Toast.makeText(this, "WIFI已连接,移动数据已连接", Toast.LENGTH_SHORT).show();
    }
    /**
     * @desc 跳转页面
     */
    public void toPageJump(Class<?> cls) {
        startActivity(new Intent(this, cls));
    }

    /**
     * @desc 权限申请 permissions  Manifest.permission.RECORD_AUDIO
     */
    public void requestPermissions(String permissions) {
        new RxPermissions(this)
                .requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE, permissions)
                .subscribe(new io.reactivex.functions.Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {     // 用户已经同意该权限
                            userAgreedPermissions();
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                            greedNextPermissions();
                        } else {   // 用户拒绝了该权限，并且选中『不再询问』
                            refusedPermissions();
                        }
                    }
                });
    }
    /**
     * @desc 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
     * */
    protected void greedNextPermissions() {

    }

    /**
     * @desc 用户拒绝了该权限 并且选中『不再询问』
     * */
    protected void refusedPermissions() {

    }
    //-------------------title设置------------------------------------------------------------------
    /**
     * @desc 显示标题栏左边的图标 一般是返回图标
     * */
    public void showTileIvLeft(){
        myBaseRlTileIvLeft.setVisibility(View.VISIBLE);
    }
    /**
     * @desc 隐藏标题栏左边的图标 一般是返回图标
     * */
    public void hiddenTileIvLeft(){
        myBaseRlTileIvLeft.setVisibility(View.GONE);
    }
    /**
     * @desc 设置标题栏左边的图标 一般是返回图标
     * @param resIcon 图标引用
     * */
    public void setIvLeftIcon(int resIcon){
        myBaseRlTileIvLeft.setBackgroundResource(resIcon);
    }

    //-------------------右边的图标------------------------------------------------------------------
    /**
     * @desc 显示标题栏右边的图标
     * */
    public void showTileIvRight(){
        myBaseRlTileIvRight.setVisibility(View.VISIBLE);
    }
    /**
     * @desc 隐藏标题栏右边的图标
     * */
    public void hiddenTileIvRight(){
        myBaseRlTileIvRight.setVisibility(View.GONE);
    }
    /**
     * @desc 设置标题栏右边的图标
     * @param resIcon 图标引用
     * */
    public void setIvLeftRight(int resIcon){
        myBaseRlTileIvRight.setBackgroundResource(resIcon);
    }


    //-------------------title背景色-----------------------------------------------------------------
    /**
     * @desc 显示title右边的文字
     * */
    public void showTilteBg(){
        myBaseRlTile.setVisibility(View.VISIBLE);
    }
    /**
     * @desc 隐藏title右边的文字
     * */
    public void hiddenTilteBg(){
        myBaseRlTile.setVisibility(View.GONE);
    }
    /**
     * @desc 修改 title背景色
     * */
    public void setTilteBgColor(int resColor){
        myBaseRlTile.setBackgroundResource(resColor);
    }


    //-------------------title-----------------------------------------------------------------
    /**
     * @desc 显示title右边的文字
     * */
    public void showTitleContent(){
        myBaseRlTileTvTile.setVisibility(View.VISIBLE);
    }
    /**
     * @desc 隐藏title右边的文字
     * */
    public void hiddenTitleContent(){
        myBaseRlTileTvTile.setVisibility(View.GONE);
    }
    /**
     * @desc 设置标题的颜色和内容
     * @param text   标题的内容
     * @param resColor  标题的颜色
     * */
    public void setTitleContent(String text,int resColor){
        myBaseRlTileTvTile.setText(text);
        myBaseRlTileTvTile.setTextColor(resColor);
    }
    /**
     * @desc 设置标题的大小
     * */
    public void setTitleContentSize(int seze){
        myBaseRlTileTvTile.setTextSize(seze);
    }

    //-------------------TODO title右边的文字-------------------------------------------------------------
    /**
     * @desc 显示title右边的文字
     * */
    public void showTilteRightTVContent(){
        myBaseRlTileTvRight.setVisibility(View.VISIBLE);
    }
    /**
     * @desc 隐藏title右边的文字
     * */
    public void hiddenTilteRightTVContent(){
        myBaseRlTileTvRight.setVisibility(View.GONE);
    }
    /**
     * @desc 设置标题右边的文字的颜色和内容
     * @param text   标题的内容
     * @param resColor  标题的颜色
     * */
    public void setTilteRightTVContent(String text,int resColor){
        myBaseRlTileTvRight.setText(text);
        myBaseRlTileTvRight.setTextColor(resColor);
    }
    /**
     * @desc 设置标题右边的文字的大小
     * */
    public void setTilteRightTVContentSize(int seze){
        myBaseRlTileTvRight.setTextSize(seze);
    }




    /**
     * @desc 用户已经同意该权限
     * */
    protected abstract void userAgreedPermissions();
    /**
     * @desc 添加布局
     * */
    protected abstract View AddLayoutView();
    /**
     * @desc 初始化数据
     * */
    protected abstract void initChildViewDate();
    // 点击 左边的图标
    protected abstract void onClickedTvRight();
    // 点击 右边的图标
    protected abstract void onClickedIvRight();
    // 点击 右边的文字
    protected abstract void onClickedIvLeft();

}
